$(function(){
    if (!_messages) {
        return;
    }
    var html = '';
    for (var i in _messages) {
        var data = _messages[i];
        var msg = data[0];
        var code = data[1];
        var type = data[2];
        var t = 'info';
        if (type == 1) {
            t = 'error';
        } else if (type == 2) {
            t = 'warning';
        } else if (type == 3) {
            t = 'info';
        } else if (type == 4) {
            t = 'success';
        }
        html += '<div class="alert alert-' + t + '">';
        html += '<button type="button" class="close" data-dismiss="alert">×</button>';
        html += msg;
        html += '</div>';
    }
    $('#message-box').html(html);
});

$.fn.category = function(config) {
    var fn = $(this);
    var data = config.data || {};
    var pid = config.pid || 0;
    var currentId = config.currentId || 0;
    var selectAll = config.selectAll ? true : false;
    var str = '<option value="0">根分类</option>' + getOption(data);
    $(this).html(str);
    function getOption(json, span, isDis) {
        var str = '';
        if (!span) {
            span = '';
        }
        span += '----';
        if (!json) {
            return str;
        }
        for (var i in json) {
            var disabled = '';
            var d = json[i];
            var selected = d.CategoryId == pid ? ' selected ' : '';

            if (currentId == d.CategoryId || isDis) {
                disabled = ' disabled';
            }

            if (selectAll) {
                var defaultId = fn.attr('defaultId');
                if (defaultId && defaultId == d.CategoryId) {
                    str += '<option class="" value="' + d.CategoryId + '"  selected>' + span + d.CategoryName +'</option>';
                } else {
                    str += '<option class="" value="' + d.CategoryId + '">' + span + d.CategoryName +'</option>';
                }
            } else {
                str += '<option class="'+disabled+'" value="' + d.CategoryId + '" ' + selected + disabled +'>' + span + d.CategoryName +'</option>';
            }

            if (d.List) {
                if (currentId == d.CategoryId || isDis) {
                    str += getOption(d.List, span, true);
                } else {
                    str += getOption(d.List, span, false);
                }
            }
        }
        return str;
    }
};
$.fn.tag = function(config) {
    config = config || {};
    var selectedBox = config.selectedBox;
    var addFunc = config.addFunc;
    var removeFunc = config.removeFunc;
    var defaultTagIds = config.defaultTagIds || [];
    var url = config.url || '/Ajax/Tag/getTag';
    var fn = $(this);

    function init() {
        fn.html('<span class="box"></span><span class="more"></span>');
        bindAdd();
        bindDel();
        getTags();
    }

    function getTags() {
        $.ajax({
            url: url,
            type: 'post',
            dataType: 'json',
            success: function(d) {
                if (d.error) {
                    alert(d.data);
                    return;
                }
                var tags = d.data.tags;
                var str = '';
                for (var i in tags) {
                    var tag = tags[i];
                    str += '<a class="tag" tagName="' + tag.TagName + '" tagId=' + tag.TagId + ' href="javascript:;"><span class="label label-info">' + tag.TagName + ' </span></a> ';
                }
                fn.find('.box').append(str);
                if (defaultTagIds) {
                    for (var i in defaultTagIds) {
                        var tagId = defaultTagIds[i];
                        fn.find('.box').find('a.tag[tagId="' + tagId + '"]').click();
                    }
                }
            }
        });
    }

    function bindAdd() {
        fn.on('click', 'a.tag', function() {
            var clone = $(this).clone();
            var tagId = clone.attr('tagId');
            var tagName = clone.attr('tagName');
            var ok = true;
            selectedBox.find('.tag').each(function() {
                var id = $(this).attr('tagId');
                if (ok && tagId == id) {
                    ok = false;
                    alert('此标签已经添加过了');
                }
            });
            if (ok) {
                clone.find('span').removeClass('label-info').addClass('label-success').append('<i class="icon-trash"></i>');
                selectedBox.append(clone);
                addFunc(tagId, tagName);
            }
        });
    }

    function bindDel() {
        selectedBox.on('click', 'a.tag', function() {
            var tagId = $(this).attr('tagId');
            var tagName = $(this).attr('tagName');
            $(this).remove();
            removeFunc(tagId, tagName);
        });
    }

    init();
};